package com.example.todo_list;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.litepal.LitePal;

import java.util.List;

import static android.content.ContentValues.TAG;

public class Myadapter extends RecyclerView.Adapter<Myadapter.ViewHolder> {
    private List <todo_list_item> mylist;
    private Context context;
    class ViewHolder extends RecyclerView.ViewHolder{
        CheckBox check;
        TextView text;
        Button delete;
        public ViewHolder(View view){
            super(view);
            check = (CheckBox)view.findViewById(R.id.check);
            text = (TextView)view.findViewById(R.id.text);
            delete = (Button)view.findViewById(R.id.delete);
        }
    }

    public Myadapter(Context context,List<todo_list_item> todo_list){
        mylist = todo_list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final int number;
        final todo_list_item temp = mylist.get(position);
        if(temp.getIs_check() == 2){
            holder.check.setChecked(true);
            holder.text.setText(temp.getMessage());
            holder.text.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }
        else{
            holder.text.setText(temp.getMessage());
            holder.text.getPaint().setFlags(0);
        }
        number = temp.getNumber();
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tempnum = ""+number;

                LitePal.deleteAll(todolist.class,"number = ?",tempnum);
                mylist.remove(position);
                notifyItemRemoved(position);
                if(position != mylist.size()){ // 如果移除的是最后一个，忽略
                    notifyItemRangeChanged(position, mylist.size() - position);
                }
            }
        });
        holder.check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过checkbox的isChecked()方法判断是否选中
                boolean checked = holder.check.isChecked();
                String tempnum = ""+number;
                todolist tempitem = new todolist();
                if(checked){
                    //Toast.makeText(context,"checked",Toast.LENGTH_SHORT).show();
                    holder.text.setText(temp.getMessage());
                    holder.text.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


                    tempitem.setIs_check(2);
                    tempitem.updateAll("number = ?",tempnum);
                }
                else{
                    holder.text.setText(temp.getMessage());
                    holder.text.getPaint().setFlags(0);
                    tempitem.setIs_check(1);
                    tempitem.updateAll("number = ?",tempnum);
                }
            }
        });
    }



    @Override
    public int getItemCount() {
        return mylist.size();
    }
}
